package me.xingyannian.hope.service.impl;

import me.xingyannian.hope.entity.User;
import me.xingyannian.hope.repository.UserRepository;
import me.xingyannian.hope.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    private UserRepository userRepository;

    public UserServiceImpl(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public User getUserById(Long id) {
        return this.userRepository.findById(id).get();
    }

    @Override
    public User create(User resources) {
        return this.userRepository.save(resources);
    }

    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids){
            if (this.userRepository.existsById(id)){
                this.userRepository.deleteById(id);
            }
        }

    }

    @Override
    public void update(User resources) {
        this.userRepository.save(resources);
    }

    @Override
    public User saveUser(User user) {
        return this.userRepository.save(user);
    }

    @Override
    public Boolean isExist(String loginId) {
        return this.userRepository.existsByLoginId(loginId);
    }

    @Override
    @Transactional(readOnly = true)
    public User getUserByLoginId(String loginId) {
        return this.userRepository.getUserByLoginId(loginId);
    }

    @Override
    public List<User> getAllUsers() {
        return this.userRepository.findAll();
    }
}
